<?php
/**
 * Generate a relative URL using a route name, and any required parameters
 *
 * @package     Visi
 * @subpackage  View Helpers
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @version     $Id$
 * @see			application/configs/routes.php for list of app routes
 * @todo		For default-style routes, this thing adds unnecessary params sometimes. Look at the backend users index for an example. Fix it.
 * @todo		Use $router->hasRoute() and $router->getRoute() rather than the in_array stuff...
 */
class Visi_View_Helper_RouteUrl extends Zend_View_Helper_Url {

    public function routeUrl($name = null, $urlOptions = array(), $getParams = array(), $reset = true, $encode = true) {
    
        $front = Zend_Controller_Front::getInstance();
        $routes = array_keys($front->getRouter()->getRoutes());
		if (is_null($urlOptions)) $urlOptions = array();
        if (!in_array($name, $routes)) {
            throw new Visi_Exception_RouteNotDefined("'$name' is not a defined route name.");
        } else {
            if (is_null($name)) $name = "default";
            $url = parent::url($urlOptions, $name, $reset, $encode);
        }
        // put together the GET parameters
        $queryString = "";
        $queryPairs = array();
        foreach ($getParams as $name => $value) {
            $queryPairs[] = "$name=$value";
        }
        if (!empty($queryPairs)) {
            $queryString = "?" . implode("&", $queryPairs);
        }
		// @todo I'm not sure if the query string should be getting escaped here or just the URL part... ?
        return $this->view->escapeUrl($url . $queryString);
    
    }

}